package com.qiaofang.controller;

import com.qiaofang.common.model.UiListBean;
import com.qiaofang.common.response.CommonResponse;
import com.qiaofang.common.response.SuccessfulResponseFactory;
import com.qiaofang.dto.*;
import com.qiaofang.request.*;
import com.qiaofang.response.*;
import com.qiaofang.service.ReportService;
import com.qiaofang.service.V10ApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@Api(tags = {"V10同步报备"})
@RequestMapping(value = "/report", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public class ReportController {

    @Autowired
    private ReportService reportService;
    @Autowired
    private V10ApiService v10ApiService;

    @PostMapping(value = "/listReport")
    @ApiOperation(value = "报备列表")
    public ListReportResponse listReport(@RequestBody ListReportRequest request) {
        ListReportResponse response = new ListReportResponse();
        ListReportDTO dto = new ListReportDTO();
//        dto.setSource(request.getSource());
        dto.setReportDeptName(request.getReportDeptName());
//        dto.setConfirmStatus(request.getConfirmStatus());
//        dto.setSyncStatus(request.getSyncStatus());
        dto.setCreateTimeStart(request.getCreateTimeStart());
        dto.setCreateTimeEnd(request.getCreateTimeEnd());
//        dto.setLastUpdateTimeStart(request.getLastUpdateTimeStart());
//        dto.setLastUpdateTimeEnd(request.getLastUpdateTimeEnd());
        dto.setSearchContent(request.getSearchContent());
        response.setData(reportService.listReport(dto, request.getPage()));
        return response;
    }

    @PostMapping(value = "/getReport")
    @ApiOperation(value = "报备详情")
    public GetReportResponse getReport(@RequestBody GetReportRequest request) {
        GetReportResponse response = new GetReportResponse();
        response.setData(reportService.getReport(request.getId()));
        return response;
    }

    @PostMapping(value = "/sendStatus")
    @ApiOperation(value = "同步V10状态")
    public CommonResponse sendStatus(@RequestBody SendStatusRequest request) {
        reportService.sendStatus(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/listCusPreHistory")
    @ApiOperation(value = "报备查询,包含备份表")
    public CommonResponse listCusPreHistory(@RequestBody ListCusPreHistoryRequest request) {
        ListCusPreHistoryResponse response = new ListCusPreHistoryResponse();

        ListCusPreHistoryDTO historyDTO = new ListCusPreHistoryDTO();
        historyDTO.setMobile(request.getMobile());
        historyDTO.setDevCompany(request.getDevCompany());
        historyDTO.setPremisesName(request.getPremisesName());
        historyDTO.setAgentCompany(request.getAgentCompany());
        historyDTO.setType(request.getType());
        historyDTO.setCreateTimeStart(request.getCreateTimeStart());
        historyDTO.setCreateTimeEnd(request.getCreateTimeEnd());
        UiListBean uiListBean = reportService.listCusPreHistory(historyDTO, request.getPage());
        response.setData(uiListBean);
        return response;
    }

    @PostMapping(value = "/listCusPreNormal")
    @ApiOperation(value = "报备查询,正常业务表数据")
    public CommonResponse listCusPreNormal(@RequestBody ListCusPreNormalRequest request) {
        ListCusPreNormalResponse response = new ListCusPreNormalResponse();

        ListCusPreNormalDTO normalDTO = new ListCusPreNormalDTO();
        normalDTO.setMobile(request.getMobile());
        normalDTO.setDevCompany(request.getDevCompany());
        normalDTO.setPremisesName(request.getPremisesName());
        normalDTO.setAgentCompany(request.getAgentCompany());
        normalDTO.setCustomerStatus(request.getCustomerStatus());
        normalDTO.setConfirmStatus(request.getConfirmStatus());
        normalDTO.setCreateTimeStart(request.getCreateTimeStart());
        normalDTO.setCreateTimeEnd(request.getCreateTimeEnd());
        UiListBean uiListBean = reportService.listCusPreNormal(normalDTO, request.getPage());
        response.setData(uiListBean);
        return response;
    }

    @PostMapping(value = "/listFollow")
    @ApiOperation(value = "报备跟进记录")
    public CommonResponse listFollow(@RequestBody ListFollowRequest request) {
        ListFollowResponse response = new ListFollowResponse();
        ListFollowDTO followDTO = new ListFollowDTO();
        followDTO.setCustomerPremisesId(request.getCustomerPremisesId());
        UiListBean uiListBean = reportService.listFollow(followDTO);
        response.setData(uiListBean);
        return response;
    }

    @PostMapping(value = "/changeStatus")
    @ApiOperation(value = "报备状态变更")
    public CommonResponse changeStatus(@RequestBody ChangeCusPreStatusRequest request){
        ChangeCusPreStatusDTO ccpsDto = new ChangeCusPreStatusDTO();
        ccpsDto.setCustomerPremisesId(request.getCustomerPremisesId());
        ccpsDto.setCustomerStatus(request.getCustomerStatus());
        ccpsDto.setRemark(request.getRemark());
        reportService.changeCusPreStatus(ccpsDto);
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/pushPremisesToV10")
    @ApiOperation(value = "同步数据接口")
    public CommonResponse listApiLog(@RequestBody SendPremisesToV10Request request) {
        return v10ApiService.pushPremisesToV10(request.getDevCompanyId(), request.getAgentCompanyId(), request.getPremisesIds());
    }


    @PostMapping(value = "/deleteCusPre")
    @ApiOperation(value = "后台运营删除报备")
    public CommonResponse deleteCusPre(@RequestBody @Valid DeleteCusPreRequest deleteCusPreRequest){
        reportService.deleteCusPre(deleteCusPreRequest.getId());

        //实际上 v10并未删除 而是转为无效报备
        v10ApiService.deleteReportToV10(deleteCusPreRequest.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }
}
